package tr.edu.yildiz.ce.dap.appagent.multicast;

import java.io.IOException;
import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.service.MessageConverter;
import tr.edu.yildiz.ce.dap.appagent.util.AgentProperties;

public class MulticastMock implements Runnable{
	
	@Override
	public void run() {
		MulticastSocket socket = null;
		try {
			socket = new MulticastSocket();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		DapMessage dapMessage = new DapMessage();
		dapMessage.setProxyAddress("127.0.0.1");
		dapMessage.setProxyPort(new BigInteger(String.valueOf(AgentProperties.getInstance().getProxyPort())));
		
		String xml = null;
		try {
			xml = MessageConverter.convertToXML(dapMessage);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		 while (true) {
		        try {
		            byte[] buf = xml.getBytes();
		            // don't wait for request...just send a quote
		            InetAddress group = InetAddress.getByName(
		                                            "230.0.0.0");
		            DatagramPacket packet;
		            packet = new DatagramPacket(buf, buf.length, 
		                                        group, 4446);
		            socket.send(packet);

		            try {
		                Thread.sleep(1000);
		            } catch (InterruptedException e) { }
		        } catch (IOException e) {
		            e.printStackTrace();
		        } 
		    }

		
	}
	
}
